home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / TERMLIB / TPUTS.C < prev    next >
C/C++ Source or Header  |  1988-08-23  |  7KB  |  261 lines

  1. /************************************************************************
  2.  *                                      *
  3.  *              Copyright (c) 1982, Fred Fish              *
  4.  *              All Rights Reserved                  *
  5.  *                                      *
  6.  *    This software and/or documentation is released for public          *
  7.  *    distribution for personal, non-commercial use only.          *
  8.  *    Limited rights to use, modify, and redistribute are hereby      *
  9.  *    granted for non-commercial purposes, provided that all          *
  10.  *    copyright notices remain intact and all changes are clearly     *
  11.  *    documented.  The author makes no warranty of any kind with      *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular     *
  14.  *    purpose.                                  *
  15.  *                                      *
  16.  ************************************************************************
  17.  */
  18. /* Modified:
  19.  * 30-Apr-86 Mic Kaczmarczik
  20.  *       - Use ctype.h macros instead of the function isdigit().
  21.  *       - Use VMS speed value for ospeed -- in microEmacs
  22.  *         this is obtainted by ttinit().
  23.  */
  24.  
  25.  
  26.  
  27.  
  28. /*
  29.  *  LIBRARY FUNCTION
  30.  *
  31.  *    tputs    output string with appropriate padding
  32.  *
  33.  *  KEY WORDS
  34.  *
  35.  *    termcap
  36.  *
  37.  *  SYNOPSIS
  38.  *
  39.  *    tputs(cp,affcnt,outc)
  40.  *    char *cp;
  41.  *    int affcnt;
  42.  *    int (*outc)();
  43.  *
  44.  *  DESCRIPTION
  45.  *
  46.  *    Outputs string pointed to by cp, using function outc, and
  47.  *    following it with the appropriate number of padding characters.
  48.  *    Affcnt contains the number of lines affected, which is used
  49.  *    as a multiplier for the specified per line pad time.  If
  50.  *    per line pad count is not applicable, affcnt should be 1,
  51.  *    NOT zero.
  52.  *
  53.  *    The format of the string pointed to by cp is:
  54.  *
  55.  *          [pad time][*]<string to send>
  56.  *
  57.  *          where:  pad time => time to delay in milliseconds
  58.  *              * => specifies that time is per line
  59.  *
  60.  *    The pad character is assumed to reside in the external
  61.  *    variable "PC".  Also, the external variable "ospeed"
  62.  *    should contain the output speed of the terminal as
  63.  *    encoded in /usr/include/sgtty.h  (B0-B9600).
  64.  *
  65.  * SYSTEM DEPENDENCIES
  66.  *
  67.  *    On VMS, the external variable "ospeed" should contain the
  68.  *    output speed of the terminal as obtained from byte 3 of
  69.  *    the iosb status buffer, using the IO$_SENSEMODE QIO.
  70.  *    The table times[] compiles into the correct values for VMS,
  71.  *    and, happily, also handles 19200 baud.
  72.  *
  73.  *  BUGS
  74.  *
  75.  *    If ospeed is 0 for some reason, there is the chance of a
  76.  *    divide by 0 operation.
  77.  *
  78.  */
  79.  
  80.  
  81.  
  82. /*
  83.  *    Miscellaneous stuff
  84.  */
  85.  
  86. #include <stdio.h>
  87. #include <ctype.h>
  88.  
  89. extern char PC;                  /* Pad character to use */
  90. extern short ospeed;          /* Encoding of output speed */
  91.  
  92. #if   VMS
  93. static int times[] = {
  94.     10000,              /* Tenths of ms per char       0 baud (bogus) */
  95.     2000,              /* Tenths of ms per char      50 baud */
  96.     1333,              /* Tenths of ms per char      75 baud */
  97.     909,              /* Tenths of ms per char     110 baud */
  98.     743,              /* Tenths of ms per char     134 baud */
  99.     666,              /* Tenths of ms per char     150 baud */
  100.     333,              /* Tenths of ms per char     300 baud */
  101.     166,              /* Tenths of ms per char     600 baud */
  102.     83,                      /* Tenths of ms per char    1200 baud */
  103.     55,                      /* Tenths of ms per char    1800 baud */
  104.     50,                      /* Tenths of ms per char    2000 baud */
  105.     41,                      /* Tenths of ms per char    2400 baud */
  106.     28,                      /* Tenths of ms per char    3600 baud */
  107.     20,                      /* Tenths of ms per char    4800 baud */
  108.     14,                      /* Tenths of ms per char    7200 baud */
  109.     10,                      /* Tenths of ms per char    9600 baud */
  110.     5                  /* Tenths of ms per char 19200 baud */
  111. };
  112. #else
  113. /* Times for Unix */
  114. static int times[] = {
  115.     0,                      /* Tenths of ms per char 0 baud */
  116.     2000,              /* Tenths of ms per char 50 baud */
  117.     1333,              /* Tenths of ms per char 75 baud */
  118.     909,              /* Tenths of ms per char 110 baud */
  119.     743,              /* Tenths of ms per char 134 baud */
  120.     666,              /* Tenths of ms per char 150 baud */
  121.     500,              /* Tenths of ms per char 200 baud */
  122.     333,              /* Tenths of ms per char 300 baud */
  123.     166,              /* Tenths of ms per char 600 baud */
  124.     83,                      /* Tenths of ms per char 1200 baud */
  125.     55,                      /* Tenths of ms per char 1800 baud */
  126.     41,                      /* Tenths of ms per char 2400 baud */
  127.     20,                      /* Tenths of ms per char 4800 baud */
  128.     10                      /* Tenths of ms per char 9600 baud */
  129. };
  130. #endif
  131.  
  132.  
  133.  
  134.  
  135. /*
  136.  *  PSEUDO CODE
  137.  *
  138.  *    Begin tgoto
  139.  *      If string pointer is invalid then
  140.  *          Return without doing anything.
  141.  *      Else
  142.  *          For each pad digit (if any)
  143.  *          Do decimal left shift.
  144.  *          Accumulate the lower digit.
  145.  *          End for
  146.  *          Adjust scale to tenths of milliseconds
  147.  *          If there is a fractional field
  148.  *          Skip the decimal point.
  149.  *          If there is a valid tenths digit
  150.  *              Accumulate the tenths.
  151.  *          End if
  152.  *          Discard remaining digits.
  153.  *          End if
  154.  *          If per line is specified then
  155.  *          Adjust the pad time.
  156.  *          Discard the per line flag char.
  157.  *          End if
  158.  *          While there are any characters left
  159.  *          Send them out via output function.
  160.  *          End while
  161.  *          Transmit any padding required.
  162.  *      End if
  163.  *    End tgoto
  164.  *
  165.  */
  166.  
  167. tputs(cp,affcnt,outc)
  168. char *cp;
  169. int affcnt;
  170. int (*outc)();
  171. {
  172.     int ptime;                  /* Pad time in tenths of milliseconds */
  173.     static do_padding();
  174.  
  175.     if (cp == NULL || *cp == NULL) {
  176.       return;
  177.     } else {
  178.       for (ptime = 0; isdigit(*cp); cp++) {
  179.       ptime *= 10;
  180.       ptime += (*cp - '0');
  181.       }
  182.       ptime *= 10;
  183.       if (*cp == '.') {
  184.       cp++;
  185.       if (isdigit(*cp)) {
  186.           ptime += (*cp++ - '0');
  187.       }
  188.       while (isdigit(*cp)) {cp++;}
  189.       }
  190.       if (*cp == '*') {
  191.       ptime *= affcnt;
  192.       cp++;
  193.       }
  194.       while (*cp != NULL) {
  195.       (*outc)(*cp++);
  196.       }
  197.       do_padding(ptime,outc);
  198.     }
  199. }
  200.  
  201.  
  202.  
  203. /*
  204.  *  FUNCTION
  205.  *
  206.  *    do_padding    transmit any pad characters required
  207.  *
  208.  *  SYNOPSIS
  209.  *
  210.  *    static do_padding(ptime,outc)
  211.  *    int ptime;
  212.  *    int (*outc)();
  213.  *
  214.  *  DESCRIPTION
  215.  *
  216.  *    Does any padding required as specified by ptime (in tenths
  217.  *    of milliseconds), the output speed given in the external
  218.  *    variable ospeed, and the pad character given in the
  219.  *    external variable PC.
  220.  *
  221.  */
  222.  
  223.  
  224.  
  225. /*
  226.  *  PSEUDO CODE
  227.  *
  228.  *    Begin do_padding
  229.  *      If there is a non-zero pad time then
  230.  *          If the external speed is in range then
  231.  *          Look up the delay per pad character.
  232.  *          Round pad time up by half a character.
  233.  *          Compute number of characters to send.
  234.  *          For each pad character to send
  235.  *              Transmit the pad character.
  236.  *          End for
  237.  *          End if
  238.  *      End if
  239.  *    End do_padding
  240.  *
  241.  */
  242.  
  243. static do_padding(ptime,outc)
  244. int ptime;
  245. int (*outc)();
  246. {
  247.     register int nchars;
  248.     register int tpc;
  249.  
  250.     if (ptime >= 0) {
  251.       if (ospeed >= 0 && ospeed <= (sizeof(times)/ sizeof(int))) {
  252.       tpc = times[ospeed];
  253.       ptime += (tpc / 2);
  254.       nchars = ptime / tpc;
  255.       for ( ; nchars > 0; --nchars) {
  256.           (*outc)(PC);
  257.       }
  258.       }
  259.     }
  260. }
  261.